<?php

/**
 * Description of StandardPage
 * Pour créer une page standard, elle permet a n'importe quel objet de surcharger de maniere assez automatisee une page de base
 * @author bonfiglio
 */
class StandardPage extends Page {

    public $display = array();
    public $View = null;
    public $Parameters = array();
    public $externalFiles = array(
        'css' => array(),
        'js' => array(),
        'js_content' => ''
    );
    public $html = '';

    public function __construct($options = array()) {
        parent::__construct($options);
    }
    public static function isValidView($View){
        return is_object($View) && method_exists($View, 'getExternalFiles') && method_exists($View, 'getContent') && method_exists($View, 'getPageProperty');
    }
    public function setView($View = false) {
        if (is_object($View)) {
            $this->View = get_class($View);
            if (StandardPage::isValidView($View)) {
                $this->mergeOptions($View->getOptions());
                $this->setDisplay($View->getPageProperty());
                $this->setHTML($View->getContent());
                $this->addExternalFiles($View->getExternalFiles());
                $this->Parameters = $View->Parameters;
            } else {
                $this->setHTML(HtmlTag::img(array('src' => $this->getFrontPath() . 'img/404.jpg', 'style' => 'display:block;width:460px;height:520;margin:10px auto;')));
            }
        }
        return $this;
    }

    public function addExternalFiles($externalFiles = array()) {
        if (is_array($externalFiles) && count($externalFiles)) {
            if (isset($externalFiles['css']) && !empty($externalFiles['css'])) {
                foreach ($externalFiles['css'] as $file) {
                    $this->externalFiles['css'][] = $file;
                }
            }
            if (isset($externalFiles['js']) && !empty($externalFiles['js'])) {
                foreach ($externalFiles['js'] as $file) {
                    $this->externalFiles['js'][] = $file;
                }
            }
            if (isset($externalFiles['js_content']) && is_string($externalFiles['js_content'])) {
                $this->externalFiles['js_content'] = $externalFiles['js_content'];
            }
        }
    }

    public function display($more = '') {
        $html = $this->getInnerHead() . $this->getInnerHTML() . (string) $more . $this->getInnerFooter();
        $additionalHead = $this->getAddditionalHead();
        $additionalFooter = $this->getAddditionalFoot();
        echo $this->getDisplay($html, $additionalHead, $additionalFooter, $bodyAttr = array());
    }

    public function getInnerHTML() {
        return $this->html;
    }

    public function getInnerFooter() {
        return '';
    }

    public function getInnerHead() {
        return '';
    }

    public function setHTML($content) {
        if (is_scalar($content)) {
            $this->html = $content;
        }
    }

    public function getAddditionalHead() {
        $return = '';
        foreach ($this->externalFiles['css'] as $file) {
            $return .= $this->getStyleSheet($file, false);
        }
        return $return;
    }

    public function getAddditionalFoot() {
        $return = '';
        foreach ($this->externalFiles['js'] as $file) {
            $return .= $this->getScript($file, false);
        }
        if (strlen($this->externalFiles['js_content'])) {
            $return .= $this->getScript(null, false, $this->externalFiles['js_content']);
        }
        return $return;
    }

    public function setDisplayProperty($property, $value) {
        if (array_key_exists($property, $this->display)) {
            $this->display[$property] = (boolean) $value;
        }
    }

    public function setDisplay($properties) {
        if (is_array($properties) && count($properties)) {
            foreach ($properties as $property => $value) {
                $this->setDisplayProperty($property, $value);
            }
        }
    }

}

?>
